package spotGUI.optionsGUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import managers.FastProperties;
import managers.OptionsManager;
import java.util.ResourceBundle;

public class SoundAndPopupEvent extends JPanel {

	private JLabel lblMessage;
	private JCheckBox chckbxSound;
	private JCheckBox chckbxPopup;
	private Component horizontalGlue;
	private JButton btnChooseSound;
	private Component horizontalStrut;
	
	private FastProperties options;
	
	
	public SoundAndPopupEvent(String name, String displayedName, boolean forcePopupEnabled) {
		setOpaque(false);
		setBackground(new Color (243, 199, 148));
		setMinimumSize(new Dimension(500, 44));
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		options = OptionsManager.getSharedInstance().getOptionGroup("soundPopup");



		ItemListener listener = new ItemListener () {
			public void itemStateChanged(ItemEvent arg0) {
				if (arg0.getStateChange() == ItemEvent.SELECTED) {
					options.setProperty(((JCheckBox)arg0.getItem()).getName(), "true");

				} else {
					options.setProperty(((JCheckBox)arg0.getItem()).getName(), "false");
				}
				options.store();
			}
		};


		ItemListener soundListener = new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				if (arg0.getStateChange() == ItemEvent.SELECTED) {
					btnChooseSound.setVisible(true);
				} else {
					btnChooseSound.setVisible(false);
				}
			}
		};

		ItemListener popupListener = new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				if (arg0.getStateChange() == ItemEvent.DESELECTED) {
					chckbxSound.setSelected(false);
					chckbxSound.setEnabled(false);
				} else {
					chckbxSound.setEnabled(true);
				}
			}
		};


		lblMessage = new JLabel(displayedName);
		lblMessage.setName(name);
		lblMessage.setIcon(new ImageIcon(SoundAndPopupEvent.class.getResource("/images/stdIcons48/next.png")));
		add(lblMessage);

		horizontalGlue = Box.createHorizontalGlue();
		add(horizontalGlue);


		btnChooseSound = new JButton(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("SoundAndPopupEvent.btnChooseSound.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnChooseSound.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new ChooseAudioDialog(null, lblMessage.getName()).setVisible(true);
			}
		});
		add(btnChooseSound);



		chckbxSound = new JCheckBox(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("SoundAndPopupEvent.chckbxSound.text")); //$NON-NLS-1$ //$NON-NLS-2$
		chckbxSound.setOpaque(false);
		chckbxSound.setName(lblMessage.getName()+".SOUND");
		chckbxSound.addItemListener(soundListener);
		chckbxSound.addItemListener(listener);
		if (options.getProperty(chckbxSound.getName()).equals("true")) {
			chckbxSound.setSelected(true);
		} else {
			chckbxSound.setSelected(false);
			btnChooseSound.setVisible(false);
		}
		add(chckbxSound);

		chckbxPopup = new JCheckBox(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("SoundAndPopupEvent.chckbxPopup.text")); //$NON-NLS-1$ //$NON-NLS-2$
		chckbxPopup.setOpaque(false);
		chckbxPopup.setName(lblMessage.getName()+".POPUP");
		chckbxPopup.addItemListener(listener);
		chckbxPopup.addItemListener(popupListener);
		if (options.getProperty(chckbxPopup.getName()).equals("true")) {
			chckbxPopup.setSelected(true);
		} else {
			chckbxPopup.setSelected(false);
		}
		add(chckbxPopup);

		horizontalStrut = Box.createHorizontalStrut(10);
		add(horizontalStrut);


		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				setOpaque(true);
				repaint();
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				setOpaque(false);
				repaint();
			}
		});

		if (forcePopupEnabled) {
			chckbxPopup.setSelected(true);
			chckbxPopup.setEnabled(false);
		}
	}

	public JCheckBox getChckbxSound() {
		return chckbxSound;
	}
	public JCheckBox getChckbxPopup() {
		return chckbxPopup;
	}
}
